﻿Imports System.ComponentModel.DataAnnotations
Imports System.ComponentModel.DataAnnotations.Schema
Imports System.Runtime.Serialization
Imports Newtonsoft.Json

Public Class Job
    Implements iCronTask

    Public Property JobId As Integer

    <Index("IX_Name", isunique:=True)> <Required> <MaxLength(100)>
    Public Property Name As String

    Public Property CategoryId As Integer?

    Public Property ForceDebug As Boolean = False
    Public Property ForceVerbose As Boolean = False
    Public Property ForceStopOnError As Boolean = False
    Public Property SingleInstance As Boolean = False

    Public Property NextRun As DateTimeOffset?
    Public Property Interval As Decimal?
    Public Property IntervalType As IntervalTypes?
    Public Property IntervalScript As String

    Public Overridable Property Category As Category
    Public Overridable Property History As ICollection(Of JobHistory)
    Public Overridable Property Steps As ICollection(Of JobStep)
    Public Overridable Property Tags As ICollection(Of JobTag)

    Public Sub New()
        Me.History = New HashSet(Of JobHistory)()
        Me.Steps = New HashSet(Of JobStep)()
        Me.Tags = New HashSet(Of JobTag)()
    End Sub
End Class

Public Class JobHistory
    Public Property JobHistoryId As Int64
    <Required>
    Public Property JobId As Integer
    <Required>
    Public Property CronGuid As Guid
    Public Property StartDTO As DateTimeOffset
    Public Property EndDTO As DateTimeOffset?

    <MaxLength(10)>
    Public Property Status As String

    Public Overridable Property Job As Job
End Class

Public Class JobStep
    Private myParameters As Hashtable
    'Public Property JobStepId As Integer

    <Required> <Key> <Column(Order:=0)>
    Public Property JobId As Integer

    <Required> <Key> <Column(Order:=1)>
    Public Property StepNumber As Integer

    <Required>
    Public Property ScriptId As Integer


    Public Property ForceDebug As Boolean = False
    Public Property ForceVerbose As Boolean = False
    Public Property ForceStopOnError As Boolean = False
    Public Property ContinueOnFailure As Boolean = False

    <NotMapped>
    Public Property Parameters As New Hashtable
    Private Property JsonParameters As String
        Get
            If Me.Parameters IsNot Nothing AndAlso Me.Parameters.Count > 0 Then
                Return JsonConvert.SerializeObject(Me.Parameters, Formatting.Indented)
            Else
                Return Nothing
            End If
        End Get
        Set(value As String)
            If value IsNot Nothing Then
                Me.Parameters = JsonConvert.DeserializeObject(Of Hashtable)(value)
            End If
        End Set
    End Property

    Public Overridable Property Job As Job
    Public Overridable Property Script As Script

    Public Sub New()
    End Sub

    Public Class JobStepConfiguration
        Inherits ModelConfiguration.EntityTypeConfiguration(Of JobStep)
        Public Sub New()
            [Property](Function(x) x.JsonParameters)
        End Sub
    End Class
End Class